﻿namespace A14_EX01_2
{
    using System;
    using A14_EX01_1;

    public class Program
    {
        public static void Main()
        {
            string currentNumber;
            int currentIntNumber = 0;
            int maximumNumber = 0;
            int minimumNumber = 0;
            int numbersCounter = 0;
            float average = 0;
            int firstNumber = 0;
            int sumOfNumbers = 0;
            int numOfNumbersDividedByFirstNumber = 0;
            bool valid = true;

            Console.WriteLine("Please enter integers. After each one press 'enter'. To Finish, enter '-1'.");
            currentNumber = Console.ReadLine();
            bool goodInput = int.TryParse(currentNumber, out currentIntNumber);
            if (goodInput)
            {
                if (currentIntNumber != -1 && currentIntNumber > -1 && currentIntNumber < 2147483647)
                {
                    sumOfNumbers += currentIntNumber;
                    average = minimumNumber = maximumNumber = firstNumber = currentIntNumber;
                    numbersCounter++;
                }
                else
                {
                    string msg = string.Format(
                                    @"Minimum number is: {0}
                                      Maximum number is: {1}
                                      Average is : {2}", 
                                    minimumNumber, maximumNumber, average);
                    Console.WriteLine(msg);
                }
            }
            else
            {
                Console.WriteLine("This is not a positive integer. Please try again:");
            }

            while (valid)
            {
                currentNumber = Console.ReadLine();
                goodInput = int.TryParse(currentNumber, out currentIntNumber);
                if (goodInput)
                {
                    if (currentIntNumber != -1 && currentIntNumber > -1 && currentIntNumber < 2147483647)
                    {
                        numbersCounter++;
                        sumOfNumbers += currentIntNumber;
                        minimumNumber = CalculatedFunctions.UpdateMinimum(minimumNumber, currentIntNumber);
                        maximumNumber = CalculatedFunctions.UpdateMaximum(maximumNumber, currentIntNumber);
                        if (CalculatedFunctions.IsDivided(firstNumber, currentIntNumber))
                        {
                            numOfNumbersDividedByFirstNumber++;
                        }
                    }
                    else
                    {
                        average = CalculatedFunctions.CalcAverage(numbersCounter, sumOfNumbers);
                        string msg = string.Format(
    @"There were {0} numbers.
Minimum number is: {1}
Maximum number is: {2}
Average is : {3}
And {4} numbers divided by the first number",
                     numbersCounter, minimumNumber, maximumNumber, average, numOfNumbersDividedByFirstNumber);
                        Console.WriteLine(msg);
                        valid = !valid;
                    }
                }
                else
                {
                    Console.WriteLine("This is not a positive integer. Please try again:");
                }
            }

            // wait for enter
            Console.WriteLine("Please press 'Enter' to exit...");
            Console.ReadLine();
        }
    }
}